package com.egao.common.module.company.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.utils.ExcelUtils;
import com.egao.common.module.company.entity.CompanyQuestion;
import com.egao.common.module.company.entity.CompanyQuestionnaireExcel;
import com.egao.common.module.company.entity.vo.CompanyStatisticsVO;
import com.egao.common.module.company.mapper.CompanyQuestionMapper;
import com.egao.common.module.company.service.CompanyQuestionService;
import com.egao.common.module.student.entity.excel.StudentPovertyExcel;
import com.egao.common.module.system.constants.DictConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

/**
 * @author warmwind
 * @since 2024-09-02 下午3:30
 */
@Service
@Slf4j
public class CompanyQuestionServiceImpl extends ServiceImpl<CompanyQuestionMapper, CompanyQuestion>
        implements CompanyQuestionService {

    @Override
    public void exportExcel(HttpServletResponse response) {
        List<CompanyQuestionnaireExcel> list = baseMapper.listExcel();

        ExcelWriter writer = ExcelUtil.getWriter(true);
        excelAddHeaderAlias(writer);

        writer.write(list, true);

        // 设置列自适应宽 必须在write以后
        ExcelUtils.autoColumnMaxSizeAll(writer.getSheet());

        String fileName = "company-questionnaire" + DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN) + ".xlsx";

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename=" + fileName);

        try {
            OutputStream out = response.getOutputStream();
            writer.flush(out, true);
            writer.close();
            IoUtil.close(out);
        } catch (IOException e) {
            writer.close();
            log.error("导出失败 - StudentPovertyServiceImpl - exportExcel", e);
        } finally {
            writer.close();
        }
    }

    @Override
    public CompanyStatisticsVO statistics() {
        return baseMapper.statistics();
    }

    /**
     * 为导出对象设置别名
     * @param writer ExcelWriter
     */
    public void excelAddHeaderAlias(ExcelWriter writer) {
        writer.addHeaderAlias("companyName", "企业名");
        writer.addHeaderAlias("yjbkbysSalary", "应届本科毕业生月薪（税前）");
        writer.addHeaderAlias("yjssbysSalary", "应届硕士毕业生月薪（税前）");
        writer.addHeaderAlias("yjbsbysSalary", "应届博士毕业生月薪（税前）");
        writer.addHeaderAlias("bkDegreesRequire", "本科学历需求");
        writer.addHeaderAlias("ssDegreesRequire", "硕士学历要求");
        writer.addHeaderAlias("bsDegreesRequire", "博士学历要求");
        writer.addHeaderAlias("majorRequire", "专业需求");
        writer.addHeaderAlias("zyzsAbility", "专业知识和技能掌握");
        writer.addHeaderAlias("gtbdAbility", "沟通表达和写作能力");
        writer.addHeaderAlias("tdxzAbility", "团队协作能力");
        writer.addHeaderAlias("cxnlAbility", "创新能力");
        writer.addHeaderAlias("kynlAbility", "抗压能力");
        writer.addHeaderAlias("sxjlAbility", "实习经历");
        writer.addHeaderAlias("empolyIssue", "应届毕业生初次入职时容易出现的问题");
        writer.addHeaderAlias("talentDirection", "贵单位认为我校在人才培养工作改进的方向有");
        writer.addHeaderAlias("promoteWorkAbility", "贵单位认为我校可以通过哪些方式帮助学生提高工作能力");
        writer.addHeaderAlias("isBuilder", "贵单位是否有意向与我校共同构建新的学生培养模式");
        writer.addHeaderAlias("builderMode", "贵单位希望采用以下哪种模式");
        writer.addHeaderAlias("opinion", "贵单位对我校人才培养和就业指导方面有哪些意见建议");
    }
}
